package union.func;

/**
 * 涓◣璁＄畻宸ュ叿绫?br/>
 * 閲囧彇鍒嗘璁＄畻鏂规硶锛?
 * 褰撴敹鍏?=800鏃?鎵ｇ◣涓?<br/>
 * 褰撴敹鍏?=4000鏃?鎵ｇ◣涓?income-800)*0.2<br/>
 * 褰撴敹鍏?=25000鏃?鎵ｇ◣涓?4000-800)*0.2+(income-4000)*0.16<br/>
 * 褰撴敹鍏?=62500鏃?鎵ｇ◣涓?4000-800)*0.2+(25000-4000)*0.16+(income-25000)*0.24<br/>
 * 褰撴敹鍏?62500鏃?鎵ｇ◣涓?4000-800)*0.2+(25000-4000)*0.16+(62500-25000)*0.24+(income-62500)*0.32<br/>
 *
 * @author skyfalling
 */
public class PersonTax {
	//鍒嗘鏀跺叆
	private static final double level1 = 800;
	private static final double level2 = 4000;
	private static final double level3 = 25000;
	private static final double level4 = 62500;
	//绋庣巼
	private static final double rate1 = 0.2;
	private static final double rate2 = 0.16;
	private static final double rate3 = 0.24;
	private static final double rate4 = 0.32;

	private static final double after1;
	private static final double after2;
	private static final double after3;
	private static final double after4;
	private static final double tax1;
	private static final double tax2;
	private static final double tax3;
	private static final double tax4;

	static {
		//涓◣閫熺畻鍥犲瓙
		tax1 = level1 * rate1;
		tax2 = level2 * rate2 - (level2 - level1) * rate1;
		tax3 = level3 * rate3 - (level2 - level1) * rate1 - (level3 - level2)
				* rate2;
		tax4 = level4 * rate4 - (level2 - level1) * rate1 - (level3 - level2)
				* rate2 - (level4 - level3) * rate3;
		//绋庡悗鏀跺叆閫熺畻鍥犲瓙
		after1 = persontax(level1);
		after2 = persontax(level2);
		after3 = persontax(level3);
		after4 = persontax(level4);
	}

	/**
	 * 鏍规嵁绋庡墠鏀跺叆璁＄畻涓◣
	 *
	 * @param income
	 * @return
	 */
	public static double taxFromIncome(Number income) {
		double value = income.doubleValue();
		return value <= level1 ? 0 : value <= level2 ? value * rate1 - tax1
				: value <= level3 ? value * rate2 - tax2
						: value <= level4 ? value * rate3 - tax3 : value
								* rate4 - tax4;
	}

	/**
	 * 鏍规嵁绋庡墠鏀跺叆璁＄畻绋庡悗鏀跺叆
	 *
	 * @param income 绋庡墠鏀跺叆
	 * @return
	 */
	public static double persontax(Number income) {
		return income.doubleValue() - taxFromIncome(income);
	}

	/**
	 * 绋庡悗鏀跺叆鎺ㄧ畻绋庡墠鏀跺叆
	 *
	 * @param afterTax
	 * @return
	 */
	public static double incomeFromIncomeAfterTax(Number afterTax) {
		double value = afterTax.doubleValue();
		return value <= after1 ? value : value <= after2 ? (value - tax1)
				/ (1 - rate1) : value <= after3 ? (value - tax2) / (1 - rate2)
				: value <= after4 ? (value - tax3) / (1 - rate3)
						: (value - tax4) / (1 - rate4);
	}

	/**
	 * 鏍规嵁绋庡悗鏀跺叆鎺ㄧ畻涓◣
	 *
	 * @param afterTax
	 * @return
	 */
	public static double taxFromIncomeAfterTax(Number afterTax) {
		return incomeFromIncomeAfterTax(afterTax) - afterTax.doubleValue();
	}

}
